GATE Exam  >  GATE Questions  >  Consider the following rec function.rec(int x... Start Learning for Free
Consider the following rec function.
rec(int x)
{
static int f;
if (x==1)
return(1);
else
f+=x*rec(x-1);
return(f);
}
Find the value returned by rec(5).
  • a)
     120
  • b)
     160
  • c)
     210
  • d)
     240
Correct answer is option 'D'. Can you explain this answer?
Verified Answer
Consider the following rec function.rec(int x){static int f;if (x==1)r...
Returning from x = 1 to 5 “f” value is updated being a static variable = 40 + 5 × 40 = 240.
View all questions of this test
Most Upvoted Answer
Consider the following rec function.rec(int x){static int f;if (x==1)r...
Recursion Explanation:
The given code snippet represents a recursive function named "rec" that takes an integer parameter "x". The function calculates the factorial of "x" using recursion.

Code Explanation:
1. The code snippet declares a static integer variable "f" within the function "rec". The static variable retains its value across function calls.
2. The function checks if the value of "x" is 1. If it is, the function returns 1 (base case) as the factorial of 1 is 1.
3. If the value of "x" is not 1, the function calculates the factorial by recursively calling itself with the parameter "x-1" and storing the result in the variable "f".
4. The factorial of "x" is then calculated as the product of "x" and the value stored in "f".
5. Finally, the calculated factorial is returned.

Calculation:
To find the value returned by "rec(5)", we trace the recursive function calls as follows:

1. rec(5)
2. 5*rec(4)
3. 5*(4*rec(3))
4. 5*(4*(3*rec(2)))
5. 5*(4*(3*(2*rec(1))))
6. 5*(4*(3*(2*1)))
7. 5*(4*(3*2))
8. 5*(4*6)
9. 5*24
10. 120

So, the value returned by "rec(5)" is 120.

Therefore, the correct answer is option 'D' (120).
Explore Courses for GATE exam
Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer?
Question Description
Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer?.
Solutions for Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer?, a detailed solution for Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? has been provided alongside types of Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following rec function.rec(int x){static int f;if (x==1)return(1);elsef+=x*rec(x-1);return(f);}Find the value returned by rec(5).a)120b)160c)210d)240Correct answer is option 'D'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev